web:add notification plugin
This commit is contained in:
parent
5fc770f62a
commit
f43406dc4a
@ -19,6 +19,7 @@ class NaivgatorDemo extends Panel {
|
|||||||
...[
|
...[
|
||||||
'AnimatorDemo',
|
'AnimatorDemo',
|
||||||
'Gobang',
|
'Gobang',
|
||||||
|
'NotificationDemo',
|
||||||
'SwitchDemo',
|
'SwitchDemo',
|
||||||
'SliderDemo',
|
'SliderDemo',
|
||||||
'NavbarDemo',
|
'NavbarDemo',
|
||||||
|
69
doric-web/dist/index.js
vendored
69
doric-web/dist/index.js
vendored
@ -6946,6 +6946,70 @@ var doric_web = (function (exports, axios, sandbox) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var NotificationCenter;
|
||||||
|
(function (NotificationCenter) {
|
||||||
|
let receivers = [];
|
||||||
|
function publish(notification) {
|
||||||
|
receivers.filter(e => e.name === notification.name).forEach(e => {
|
||||||
|
e.callback(notification.data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
NotificationCenter.publish = publish;
|
||||||
|
function subscribe(receiver) {
|
||||||
|
receivers.push(receiver);
|
||||||
|
}
|
||||||
|
NotificationCenter.subscribe = subscribe;
|
||||||
|
function unsubscribe(receiver) {
|
||||||
|
receivers = receivers.filter(e => e !== receiver);
|
||||||
|
}
|
||||||
|
NotificationCenter.unsubscribe = unsubscribe;
|
||||||
|
})(NotificationCenter || (NotificationCenter = {}));
|
||||||
|
class NotificationPlugin extends DoricPlugin {
|
||||||
|
constructor() {
|
||||||
|
super(...arguments);
|
||||||
|
this.receivers = {};
|
||||||
|
}
|
||||||
|
publish(args) {
|
||||||
|
const key = `__doric__${args.biz || ""}#${args.name}`;
|
||||||
|
NotificationCenter.publish({
|
||||||
|
name: key,
|
||||||
|
data: !!args.data ? JSON.parse(args.data) : undefined
|
||||||
|
});
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
subscribe(args) {
|
||||||
|
const key = `__doric__${args.biz || ""}#${args.name}`;
|
||||||
|
const receiver = {
|
||||||
|
name: key,
|
||||||
|
callback: (data) => {
|
||||||
|
sandbox.jsCallResolve(this.context.contextId, args.callback, data);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.receivers[args.callback] = receiver;
|
||||||
|
NotificationCenter.subscribe(receiver);
|
||||||
|
return args.callback;
|
||||||
|
}
|
||||||
|
unsubscribe(subscribeId) {
|
||||||
|
const recevier = this.receivers[subscribeId];
|
||||||
|
if (recevier) {
|
||||||
|
NotificationCenter.unsubscribe(recevier);
|
||||||
|
this.receivers[subscribeId] = undefined;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onTearDown() {
|
||||||
|
Object.entries(this.receivers).map(e => e[1]).filter(e => !!e).forEach(e => {
|
||||||
|
if (e) {
|
||||||
|
NotificationCenter.unsubscribe(e);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
this.receivers = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const bundles = new Map;
|
const bundles = new Map;
|
||||||
const plugins = new Map;
|
const plugins = new Map;
|
||||||
const nodes = new Map;
|
const nodes = new Map;
|
||||||
@ -6973,6 +7037,7 @@ var doric_web = (function (exports, axios, sandbox) {
|
|||||||
registerPlugin('navigator', NavigatorPlugin);
|
registerPlugin('navigator', NavigatorPlugin);
|
||||||
registerPlugin('popover', PopoverPlugin);
|
registerPlugin('popover', PopoverPlugin);
|
||||||
registerPlugin('animate', AnimatePlugin);
|
registerPlugin('animate', AnimatePlugin);
|
||||||
|
registerPlugin('notification', NotificationPlugin);
|
||||||
registerViewNode('Stack', DoricStackNode);
|
registerViewNode('Stack', DoricStackNode);
|
||||||
registerViewNode('VLayout', DoricVLayoutNode);
|
registerViewNode('VLayout', DoricVLayoutNode);
|
||||||
registerViewNode('HLayout', DoricHLayoutNode);
|
registerViewNode('HLayout', DoricHLayoutNode);
|
||||||
@ -7078,7 +7143,9 @@ ${content}
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if (ret !== undefined) {
|
else if (ret !== undefined) {
|
||||||
sandbox.jsCallResolve(contextId, callbackId, ret);
|
Promise.resolve(ret).then((ret) => {
|
||||||
|
sandbox.jsCallResolve(contextId, callbackId, ret);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
|
2
doric-web/dist/index.js.map
vendored
2
doric-web/dist/index.js.map
vendored
File diff suppressed because one or more lines are too long
@ -1,4 +1,4 @@
|
|||||||
import { jsCallResolve, jsCallReject, jsCallbackTimer, jsCallEntityMethod, jsReleaseContext } from 'doric/src/runtime/sandbox'
|
import { jsCallResolve, jsCallReject, jsCallbackTimer, jsReleaseContext } from 'doric/src/runtime/sandbox'
|
||||||
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
|
import { acquireJSBundle, acquirePlugin } from './DoricRegistry'
|
||||||
import { getDoricContext } from './DoricContext'
|
import { getDoricContext } from './DoricContext'
|
||||||
import { DoricPlugin } from './DoricPlugin'
|
import { DoricPlugin } from './DoricPlugin'
|
||||||
@ -104,7 +104,9 @@ function initDoric() {
|
|||||||
jsCallReject(contextId, callbackId, e)
|
jsCallReject(contextId, callbackId, e)
|
||||||
})
|
})
|
||||||
} else if (ret !== undefined) {
|
} else if (ret !== undefined) {
|
||||||
jsCallResolve(contextId, callbackId, ret)
|
Promise.resolve(ret).then((ret) => {
|
||||||
|
jsCallResolve(contextId, callbackId, ret)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
@ -19,6 +19,7 @@ import { AnimatePlugin } from "./plugins/AnimatePlugin"
|
|||||||
import { DoricSwitchNode } from "./shader/DoricSwitchNode"
|
import { DoricSwitchNode } from "./shader/DoricSwitchNode"
|
||||||
import { DoricSliderNode } from "./shader/DoricSliderNode"
|
import { DoricSliderNode } from "./shader/DoricSliderNode"
|
||||||
import { DoricSlideItemNode } from "./shader/DoricSlideItemNode"
|
import { DoricSlideItemNode } from "./shader/DoricSlideItemNode"
|
||||||
|
import { NotificationPlugin } from "./plugins/NotificationPlugin"
|
||||||
|
|
||||||
const bundles: Map<string, string> = new Map
|
const bundles: Map<string, string> = new Map
|
||||||
|
|
||||||
@ -59,6 +60,7 @@ registerPlugin('storage', StoragePlugin)
|
|||||||
registerPlugin('navigator', NavigatorPlugin)
|
registerPlugin('navigator', NavigatorPlugin)
|
||||||
registerPlugin('popover', PopoverPlugin)
|
registerPlugin('popover', PopoverPlugin)
|
||||||
registerPlugin('animate', AnimatePlugin)
|
registerPlugin('animate', AnimatePlugin)
|
||||||
|
registerPlugin('notification', NotificationPlugin)
|
||||||
|
|
||||||
registerViewNode('Stack', DoricStackNode)
|
registerViewNode('Stack', DoricStackNode)
|
||||||
registerViewNode('VLayout', DoricVLayoutNode)
|
registerViewNode('VLayout', DoricVLayoutNode)
|
||||||
|
83
doric-web/src/plugins/NotificationPlugin.ts
Normal file
83
doric-web/src/plugins/NotificationPlugin.ts
Normal file
@ -0,0 +1,83 @@
|
|||||||
|
import { jsCallResolve } from "doric/src/runtime/sandbox";
|
||||||
|
import { DoricPlugin } from "../DoricPlugin";
|
||||||
|
|
||||||
|
namespace NotificationCenter {
|
||||||
|
type Notification = {
|
||||||
|
name: string,
|
||||||
|
data?: object
|
||||||
|
}
|
||||||
|
export type Receiver = {
|
||||||
|
name: string,
|
||||||
|
callback: (data?: object) => void
|
||||||
|
}
|
||||||
|
let receivers: Receiver[] = []
|
||||||
|
|
||||||
|
export function publish(notification: Notification) {
|
||||||
|
receivers.filter(e => e.name === notification.name).forEach(e => {
|
||||||
|
e.callback(notification.data)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
export function subscribe(receiver: Receiver) {
|
||||||
|
receivers.push(receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unsubscribe(receiver: Receiver) {
|
||||||
|
receivers = receivers.filter(e => e !== receiver)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export class NotificationPlugin extends DoricPlugin {
|
||||||
|
private receivers: Record<string, NotificationCenter.Receiver | undefined> = {}
|
||||||
|
|
||||||
|
publish(args: {
|
||||||
|
biz?: string,
|
||||||
|
name: string,
|
||||||
|
data?: string,
|
||||||
|
}) {
|
||||||
|
const key = `__doric__${args.biz || ""}#${args.name}`
|
||||||
|
NotificationCenter.publish({
|
||||||
|
name: key,
|
||||||
|
data: !!args.data ? JSON.parse(args.data) : undefined
|
||||||
|
})
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
subscribe(args: {
|
||||||
|
biz?: string,
|
||||||
|
name: string,
|
||||||
|
callback: string,
|
||||||
|
}) {
|
||||||
|
const key = `__doric__${args.biz || ""}#${args.name}`
|
||||||
|
const receiver = {
|
||||||
|
name: key,
|
||||||
|
callback: (data?: object) => {
|
||||||
|
jsCallResolve(this.context.contextId, args.callback, data)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.receivers[args.callback] = receiver
|
||||||
|
NotificationCenter.subscribe(receiver)
|
||||||
|
return args.callback
|
||||||
|
}
|
||||||
|
|
||||||
|
unsubscribe(subscribeId: string) {
|
||||||
|
const recevier = this.receivers[subscribeId]
|
||||||
|
if (recevier) {
|
||||||
|
NotificationCenter.unsubscribe(recevier)
|
||||||
|
this.receivers[subscribeId] = undefined
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onTearDown() {
|
||||||
|
Object.entries(this.receivers).map(e => e[1]).filter(e => !!e).forEach(e => {
|
||||||
|
if (e) {
|
||||||
|
NotificationCenter.unsubscribe(e)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
this.receivers = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Reference in New Issue
Block a user