Skip to content

Commit

Permalink
nodom: support storage (#1064)
Browse files Browse the repository at this point in the history
* nodom: support storage

* Fix size and update test.

* ensure all hydrate functions have the same signature

* improve whitespacing

* nitty nits

* type-alias ala erwinm

* lil fix
  • Loading branch information
samouri committed Jun 23, 2021
1 parent b1f1658 commit 5d4d4c9
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/test/main-thread/initialize-storage.test.ts
Expand Up @@ -15,7 +15,8 @@
*/

import anyTest, { TestInterface } from 'ava';
import { initialize, WorkerStorageInit } from '../../worker-thread/initialize';
import { initialize } from '../../worker-thread/initialize';
import { WorkerStorageInit } from '../../worker-thread/initialize-storage';
import { Document } from '../../worker-thread/dom/Document';
import { HydrateableNode } from '../../transfer/TransferrableNodes';

Expand Down
30 changes: 30 additions & 0 deletions src/worker-thread/hydrate.d.ts
@@ -0,0 +1,30 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* 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 type { Document } from './dom/Document';
import type { HydrateableNode } from '../transfer/TransferrableNodes';
import type { WorkerStorageInit } from './initialize-storage';

export type HydrateFunction = (
document: Document,
strings: Array<string>,
hydrateableNode: HydrateableNode,
cssKeys: Array<string>,
globalEventHandlerKeys: Array<string>,
[innerWidth, innerHeight]: [number, number],
localStorageInit: WorkerStorageInit,
sessionStorageInit: WorkerStorageInit,
) => void;
3 changes: 2 additions & 1 deletion src/worker-thread/index.amp.ts
Expand Up @@ -63,6 +63,7 @@ import { GlobalScope, WorkerDOMGlobalScope } from './WorkerDOMGlobalScope';
import { initialize } from './initialize';
import { rafPolyfill, cafPolyfill } from './AnimationFrame';
import { wrap as longTaskWrap } from './long-task';
import { HydrateFunction } from './hydrate';

declare const WORKER_DOM_DEBUG: boolean;

Expand Down Expand Up @@ -163,4 +164,4 @@ export const workerDOM: WorkerDOMGlobalScope = (function (postMessage, addEventL
(self as any).exportFunction = exportFunction;
addEventListener('message', (evt: MessageEvent) => callFunctionMessageHandler(evt, workerDOM.document));

export const hydrate = initialize;
export const hydrate: HydrateFunction = initialize;
16 changes: 15 additions & 1 deletion src/worker-thread/index.nodom.amp.ts
Expand Up @@ -15,11 +15,14 @@
*/

import type { WorkerNoDOMGlobalScope } from './WorkerDOMGlobalScope';
import type { HydrateFunction } from './hydrate';
import type { WorkerStorageInit } from './initialize-storage';

import { AMP } from './amp/amp';
import { DocumentStub } from './dom/DocumentLite';
import { callFunctionMessageHandler, exportFunction } from './function';
import { deleteGlobals } from './amp/delete-globals';
import { initializeStorage } from './initialize-storage';

const noop = () => void 0;

Expand All @@ -43,4 +46,15 @@ deleteGlobals(self);
(self as any).exportFunction = exportFunction;
addEventListener('message', (evt: MessageEvent) => callFunctionMessageHandler(evt, workerDOM.document));

export const hydrate = noop;
export const hydrate: HydrateFunction = (
document: DocumentStub,
strings: Object,
hydrateableNode: Object,
cssKeys: Object,
globalEventHandlerKeys: Object,
size: Object,
localStorageInit: WorkerStorageInit,
sessionStorageInit: WorkerStorageInit,
) => {
initializeStorage(document, localStorageInit, sessionStorageInit);
};
3 changes: 2 additions & 1 deletion src/worker-thread/index.ts
Expand Up @@ -59,6 +59,7 @@ import { Document } from './dom/Document';
import { initialize } from './initialize';
import { Event as WorkerDOMEvent } from './Event';
import { rafPolyfill, cafPolyfill } from './AnimationFrame';
import { HydrateFunction } from './hydrate';

const globalScope: GlobalScope = {
innerWidth: 0,
Expand Down Expand Up @@ -131,4 +132,4 @@ export const workerDOM = (function (postMessage, addEventListener, removeEventLi
return document.defaultView;
})(postMessage.bind(self) || noop, addEventListener.bind(self) || noop, removeEventListener.bind(self) || noop);

export const hydrate = initialize;
export const hydrate: HydrateFunction = initialize;
39 changes: 39 additions & 0 deletions src/worker-thread/initialize-storage.ts
@@ -0,0 +1,39 @@
/**
* Copyright 2021 The AMP HTML Authors. All Rights Reserved.
*
* 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 type { Document } from './dom/Document';
import type { DocumentStub } from './dom/DocumentLite';

import { createStorage } from './Storage';
import { StorageLocation } from '../transfer/TransferrableStorage';

type InitStorageMap = { storage: { [key: string]: string }; errorMsg: null };
type InitStorageError = { storage: null; errorMsg: string };
export type WorkerStorageInit = InitStorageMap | InitStorageError;

export function initializeStorage(document: Document | DocumentStub, localStorageInit: WorkerStorageInit, sessionStorageInit: WorkerStorageInit) {
const window = document.defaultView;
if (localStorageInit.storage) {
window.localStorage = createStorage(document, StorageLocation.Local, localStorageInit.storage);
} else {
console.warn(localStorageInit.errorMsg);
}
if (sessionStorageInit.storage) {
window.sessionStorage = createStorage(document, StorageLocation.Session, sessionStorageInit.storage);
} else {
console.warn(sessionStorageInit.errorMsg);
}
}
17 changes: 3 additions & 14 deletions src/worker-thread/initialize.ts
Expand Up @@ -16,15 +16,13 @@

import type { Document } from './dom/Document';
import type { HydrateableNode } from '../transfer/TransferrableNodes';
import type { WorkerStorageInit } from './initialize-storage';

import { store as storeString } from './strings';
import { TransferrableKeys } from '../transfer/TransferrableKeys';
import { appendKeys as addCssKeys } from './css/CSSStyleDeclaration';
import { createStorage } from './Storage';
import { StorageLocation } from '../transfer/TransferrableStorage';
import { appendGlobalEventProperties } from './dom/HTMLElement';

export type WorkerStorageInit = { storage: { [key: string]: string }; errorMsg: null } | { storage: null; errorMsg: string };
import { initializeStorage } from './initialize-storage';

export function initialize(
document: Document,
Expand All @@ -45,14 +43,5 @@ export function initialize(
const window = document.defaultView;
window.innerWidth = innerWidth;
window.innerHeight = innerHeight;
if (localStorageInit.storage) {
window.localStorage = createStorage(document, StorageLocation.Local, localStorageInit.storage);
} else {
console.warn(localStorageInit.errorMsg);
}
if (sessionStorageInit.storage) {
window.sessionStorage = createStorage(document, StorageLocation.Session, sessionStorageInit.storage);
} else {
console.warn(sessionStorageInit.errorMsg);
}
initializeStorage(document, localStorageInit, sessionStorageInit);
}

0 comments on commit 5d4d4c9

Please sign in to comment.