web:add notification plugin

This commit is contained in:
pengfei.zhou 2021-04-30 16:47:11 +08:00 committed by osborn
parent 5fc770f62a
commit f43406dc4a
6 changed files with 159 additions and 4 deletions

View File

@ -19,6 +19,7 @@ class NaivgatorDemo extends Panel {
...[ ...[
'AnimatorDemo', 'AnimatorDemo',
'Gobang', 'Gobang',
'NotificationDemo',
'SwitchDemo', 'SwitchDemo',
'SliderDemo', 'SliderDemo',
'NavbarDemo', 'NavbarDemo',

View File

@ -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) {
Promise.resolve(ret).then((ret) => {
sandbox.jsCallResolve(contextId, callbackId, ret); sandbox.jsCallResolve(contextId, callbackId, ret);
});
} }
return true; return true;
}); });

File diff suppressed because one or more lines are too long

View File

@ -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) {
Promise.resolve(ret).then((ret) => {
jsCallResolve(contextId, callbackId, ret) jsCallResolve(contextId, callbackId, ret)
})
} }
return true return true
}) })

View File

@ -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)

View 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 = {}
}
}