From b520ffb5df50c97bdc5befb41cbd207e68f621df Mon Sep 17 00:00:00 2001 From: "pengfei.zhou" Date: Wed, 8 Jan 2020 19:44:24 +0800 Subject: [PATCH] js:add notification plugin --- doric-js/bundle/doric-sandbox.js | 11 ++++++++ doric-js/bundle/doric-vm.js | 11 ++++++++ doric-js/lib/src/native/notification.d.ts | 16 +++++++++++ doric-js/lib/src/native/notification.js | 18 ++++++++++++ doric-js/lib/src/runtime/sandbox.d.ts | 2 ++ doric-js/lib/src/runtime/sandbox.js | 11 ++++++++ doric-js/src/native/index.native.ts | 1 + doric-js/src/native/notification.ts | 34 +++++++++++++++++++++++ doric-js/src/runtime/global.ts | 4 ++- doric-js/src/runtime/sandbox.ts | 12 ++++++++ 10 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 doric-js/lib/src/native/notification.d.ts create mode 100644 doric-js/lib/src/native/notification.js create mode 100644 doric-js/src/native/notification.ts diff --git a/doric-js/bundle/doric-sandbox.js b/doric-js/bundle/doric-sandbox.js index 47ba0706..29c492f9 100644 --- a/doric-js/bundle/doric-sandbox.js +++ b/doric-js/bundle/doric-sandbox.js @@ -1310,6 +1310,17 @@ var doric = (function (exports) { register(instance) { this.entity = instance; } + function2Id(func) { + const functionId = uniqueId('function'); + this.callbacks.set(functionId, { + resolve: func, + reject: () => { loge("This should not be called"); } + }); + return functionId; + } + removeFuncById(funcId) { + this.callbacks.delete(funcId); + } } const gContexts = new Map; const gModules = new Map; diff --git a/doric-js/bundle/doric-vm.js b/doric-js/bundle/doric-vm.js index 91711eb3..993e56c9 100644 --- a/doric-js/bundle/doric-vm.js +++ b/doric-js/bundle/doric-vm.js @@ -1333,6 +1333,17 @@ class Context { register(instance) { this.entity = instance; } + function2Id(func) { + const functionId = uniqueId('function'); + this.callbacks.set(functionId, { + resolve: func, + reject: () => { loge("This should not be called"); } + }); + return functionId; + } + removeFuncById(funcId) { + this.callbacks.delete(funcId); + } } const gContexts = new Map; const gModules = new Map; diff --git a/doric-js/lib/src/native/notification.d.ts b/doric-js/lib/src/native/notification.d.ts new file mode 100644 index 00000000..f1cb6b7d --- /dev/null +++ b/doric-js/lib/src/native/notification.d.ts @@ -0,0 +1,16 @@ +import { BridgeContext } from "../runtime/global"; +export declare function notification(context: BridgeContext): { + publish: (args: { + biz: string; + name: string; + data?: object | undefined; + androidSystem?: boolean | undefined; + }) => Promise; + subscribe: (args: { + biz: string; + name: string; + callback: (data?: object | undefined) => void; + androidSystem?: boolean | undefined; + }) => Promise; + unsubscribe: (subscribeId: string) => Promise; +}; diff --git a/doric-js/lib/src/native/notification.js b/doric-js/lib/src/native/notification.js new file mode 100644 index 00000000..5c8acc96 --- /dev/null +++ b/doric-js/lib/src/native/notification.js @@ -0,0 +1,18 @@ +export function notification(context) { + return { + publish: (args) => { + if (args.data !== undefined) { + args.data = JSON.stringify(args.data); + } + return context.notification.publish(args); + }, + subscribe: (args) => { + args.callback = context.function2Id(args.callback); + return context.notification.subscribe(args); + }, + unsubscribe: (subscribeId) => { + context.removeFuncById(subscribeId); + return context.notification.unsubscribe({ subscribeId }); + } + }; +} diff --git a/doric-js/lib/src/runtime/sandbox.d.ts b/doric-js/lib/src/runtime/sandbox.d.ts index 774d3a31..14c28c28 100644 --- a/doric-js/lib/src/runtime/sandbox.d.ts +++ b/doric-js/lib/src/runtime/sandbox.d.ts @@ -13,6 +13,8 @@ export declare class Context { constructor(id: string); callNative(namespace: string, method: string, args?: any): Promise; register(instance: Object): void; + function2Id(func: Function): string; + removeFuncById(funcId: string): void; } export declare function jsObtainContext(id: string): Context | undefined; export declare function jsReleaseContext(id: string): void; diff --git a/doric-js/lib/src/runtime/sandbox.js b/doric-js/lib/src/runtime/sandbox.js index fe3f4074..332a9348 100644 --- a/doric-js/lib/src/runtime/sandbox.js +++ b/doric-js/lib/src/runtime/sandbox.js @@ -128,6 +128,17 @@ export class Context { register(instance) { this.entity = instance; } + function2Id(func) { + const functionId = uniqueId('function'); + this.callbacks.set(functionId, { + resolve: func, + reject: () => { loge("This should not be called"); } + }); + return functionId; + } + removeFuncById(funcId) { + this.callbacks.delete(funcId); + } } const gContexts = new Map; const gModules = new Map; diff --git a/doric-js/src/native/index.native.ts b/doric-js/src/native/index.native.ts index 777af1e3..85a1ff23 100644 --- a/doric-js/src/native/index.native.ts +++ b/doric-js/src/native/index.native.ts @@ -20,3 +20,4 @@ export * from './network' export * from './storage' export * from './popover' export * from './animate' +export * from './notification' diff --git a/doric-js/src/native/notification.ts b/doric-js/src/native/notification.ts new file mode 100644 index 00000000..e189ab88 --- /dev/null +++ b/doric-js/src/native/notification.ts @@ -0,0 +1,34 @@ +/* + * Copyright [2019] [Doric.Pub] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { BridgeContext } from "../runtime/global" +export function notification(context: BridgeContext) { + return { + publish: (args: { biz: string, name: string, data?: object, androidSystem?: boolean }) => { + if (args.data !== undefined) { + (args as any).data = JSON.stringify(args.data) + } + return context.notification.publish(args) + }, + subscribe: (args: { biz: string, name: string, callback: (data?: object) => void, androidSystem?: boolean }) => { + (args as any).callback = (context as any).function2Id(args.callback) + return context.notification.subscribe(args) as Promise + }, + unsubscribe: (subscribeId: string) => { + (context as any).removeFuncById(subscribeId) + return context.notification.unsubscribe({ subscribeId }) + } + } +} \ No newline at end of file diff --git a/doric-js/src/runtime/global.ts b/doric-js/src/runtime/global.ts index 5ba6cdf4..cddeccb8 100644 --- a/doric-js/src/runtime/global.ts +++ b/doric-js/src/runtime/global.ts @@ -15,7 +15,9 @@ */ export * from 'reflect-metadata' -export type BridgeContext = { [index: string]: { [index: string]: (args?: any) => Promise } } +export type BridgeContext = { + [index: string]: { [index: string]: (args?: any) => Promise } +} declare global { const context: BridgeContext diff --git a/doric-js/src/runtime/sandbox.ts b/doric-js/src/runtime/sandbox.ts index d59a1a01..36e54194 100644 --- a/doric-js/src/runtime/sandbox.ts +++ b/doric-js/src/runtime/sandbox.ts @@ -173,6 +173,18 @@ export class Context { register(instance: Object) { this.entity = instance } + + function2Id(func: Function) { + const functionId = uniqueId('function') + this.callbacks.set(functionId, { + resolve: func, + reject: () => { loge("This should not be called") } + }) + return functionId + } + removeFuncById(funcId: string) { + this.callbacks.delete(funcId) + } }