mirror of
https://github.com/aspnet/JavaScriptServices.git
synced 2025-12-22 17:47:53 +00:00
Use external domain-tasks NPM module
This commit is contained in:
@@ -1,49 +0,0 @@
|
||||
const domain = require('domain') as any;
|
||||
const domainContext = require('domain-context') as any;
|
||||
const domainTasksStateKey = '__DOMAIN_TASKS';
|
||||
|
||||
export function addTask(task: PromiseLike<any>) {
|
||||
if (task && domain.active) {
|
||||
const state = domainContext.get(domainTasksStateKey) as DomainTasksState;
|
||||
if (state) {
|
||||
state.numRemainingTasks++;
|
||||
task.then(() => {
|
||||
// The application may have other listeners chained to this promise *after*
|
||||
// this listener. Since we don't want the combined task to complete until
|
||||
// all the handlers for child tasks have finished, delay the following by
|
||||
// one tick.
|
||||
setTimeout(() => {
|
||||
state.numRemainingTasks--;
|
||||
if (state.numRemainingTasks === 0) {
|
||||
state.triggerResolved();
|
||||
}
|
||||
}, 0);
|
||||
}, state.triggerRejected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function run(codeToRun: () => void): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
domainContext.runInNewDomain(() => {
|
||||
const state: DomainTasksState = {
|
||||
numRemainingTasks: 0,
|
||||
triggerResolved: resolve,
|
||||
triggerRejected: reject
|
||||
};
|
||||
domainContext.set(domainTasksStateKey, state);
|
||||
codeToRun();
|
||||
|
||||
// If no tasks were registered synchronously, then we're done already
|
||||
if (state.numRemainingTasks === 0) {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
}) as any as Promise<void>;
|
||||
}
|
||||
|
||||
interface DomainTasksState {
|
||||
numRemainingTasks: number;
|
||||
triggerResolved: () => void;
|
||||
triggerRejected: () => void;
|
||||
}
|
||||
@@ -1,3 +0,0 @@
|
||||
declare module 'isomorphic-fetch' {
|
||||
export default function fetch(url: string, opts: any): Promise<any>;
|
||||
}
|
||||
@@ -1,8 +1,9 @@
|
||||
require('./require-ts-babel')(); // Enable loading TS/TSX/JSX/ES2015 modules
|
||||
var url = require('url');
|
||||
var domainTasks = require('./domain-tasks.ts');
|
||||
var domainTasks = require('domain-tasks');
|
||||
var baseUrl = require('domain-tasks/fetch').baseUrl;
|
||||
|
||||
function render(bootModulePath, requestUrl, callback) {
|
||||
function render(bootModulePath, absoluteRequestUrl, requestPathAndQuery, callback) {
|
||||
var bootFunc = require(bootModulePath);
|
||||
if (typeof bootFunc !== 'function') {
|
||||
bootFunc = bootFunc.default;
|
||||
@@ -12,13 +13,15 @@ function render(bootModulePath, requestUrl, callback) {
|
||||
}
|
||||
|
||||
var params = {
|
||||
location: url.parse(requestUrl),
|
||||
url: requestUrl,
|
||||
location: url.parse(requestPathAndQuery),
|
||||
url: requestPathAndQuery,
|
||||
state: undefined
|
||||
};
|
||||
|
||||
// Open a new domain that can track all the async tasks commenced during first render
|
||||
domainTasks.run(function() {
|
||||
baseUrl(absoluteRequestUrl);
|
||||
|
||||
// Since route matching is asynchronous, add the rendering itself to the list of tasks we're awaiting
|
||||
domainTasks.addTask(new Promise(function (resolve, reject) {
|
||||
// Now actually perform the first render that will match a route and commence associated tasks
|
||||
@@ -33,19 +36,16 @@ function render(bootModulePath, requestUrl, callback) {
|
||||
}
|
||||
});
|
||||
}));
|
||||
}).then(function() {
|
||||
}, function(error) {
|
||||
// By now, all the data should be loaded, so we can render for real based on the state now
|
||||
// TODO: Add an optimisation where, if domain-tasks had no outstanding tasks at the end of
|
||||
// the previous render, we don't re-render (we can use the previous html and state).
|
||||
if (error) { console.error(error); throw error; }
|
||||
bootFunc(params, callback);
|
||||
}).catch(function(error) {
|
||||
process.nextTick(() => { // Because otherwise you can't throw from inside a catch
|
||||
callback(error, null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
render('../boot-server.tsx', '/', (err, html) => {
|
||||
render('../boot-server.tsx', 'http://localhost:5000', '/', (err, html) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
import isomorphicFetch from 'isomorphic-fetch';
|
||||
import { addTask } from './domain-tasks';
|
||||
|
||||
export function fetch(url: string): Promise<any> {
|
||||
// TODO: Find some way to supply the base URL via domain context
|
||||
var promise = isomorphicFetch('http://localhost:5000' + url, {
|
||||
headers: {
|
||||
Connection: 'keep-alive'
|
||||
}
|
||||
});
|
||||
addTask(promise);
|
||||
return promise;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fetch } from '../fx/tracked-fetch';
|
||||
import { fetch } from 'domain-tasks/fetch';
|
||||
import { typeName, isActionType, Action, Reducer } from '../fx/TypedRedux';
|
||||
import { ActionCreator } from './';
|
||||
import { Genre } from './GenreList';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fetch } from '../fx/tracked-fetch';
|
||||
import { fetch } from 'domain-tasks/fetch';
|
||||
import { typeName, isActionType, Action, Reducer } from '../fx/TypedRedux';
|
||||
import { ActionCreator } from './';
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fetch } from '../fx/tracked-fetch';
|
||||
import { fetch } from 'domain-tasks/fetch';
|
||||
import { typeName, isActionType, Action, Reducer } from '../fx/TypedRedux';
|
||||
import { ActionCreator } from './';
|
||||
import { Album } from './FeaturedAlbums';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fetch } from '../fx/tracked-fetch';
|
||||
import { fetch } from 'domain-tasks/fetch';
|
||||
import { typeName, isActionType, Action, Reducer } from '../fx/TypedRedux';
|
||||
import { ActionCreator } from './';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user