Skip to content

Commit

Permalink
Rename Cacheable -> Capturable
Browse files Browse the repository at this point in the history
  • Loading branch information
eoghanmurray committed Mar 25, 2024
1 parent a3ce3e0 commit a88ff5e
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion packages/rrdom/src/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function diffProps(
} else if (newTree.tagName === 'IFRAME' && name === 'srcdoc') continue;
else oldTree.setAttribute(name, newValue);

if (assetManager?.isCacheable(oldTree, name, newValue)) {
if (assetManager?.isCapturable(oldTree, name, newValue)) {
// can possibly remove the attribute again if it hasn't loaded yet
assetManager.manageAttribute(oldTree, rrnodeMirror.getId(newTree), name);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/rrdom/test/diff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,12 +468,12 @@ describe('diff algorithm for rrdom', () => {
beforeEach(() => {
assetManager = fromPartial({
manageAttribute: jest.fn(),
isCacheable: jest.fn(),
isCapturable: jest.fn(),
});
replayer.assetManager = assetManager;
});

it('new properties are managed by asset manager if cacheable', () => {
it('new properties are managed by asset manager if capturable', () => {
const tagName = 'IMG';
const node = document.createElement(tagName);
const sn = Object.assign({}, elementSn, { tagName });
Expand All @@ -484,7 +484,7 @@ describe('diff algorithm for rrdom', () => {
const sn2 = Object.assign({}, elementSn, { tagName });
rrDocument.mirror.add(rrNode, sn2);

(assetManager.isCacheable as jest.Mock)
(assetManager.isCapturable as jest.Mock)
.mockReturnValueOnce(true)
.mockReturnValue(false);
rrNode.attributes = { src: 'image.png', class: 'node' };
Expand Down
4 changes: 2 additions & 2 deletions packages/rrweb-snapshot/src/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getInputType,
toLowerCase,
getUrlsFromSrcset,
isAttributeCacheable,
isAttributeCapturable,
} from './utils';

let _id = 1;
Expand Down Expand Up @@ -680,7 +680,7 @@ function serializeElementNode(
));

// save assets offline
if (value && onAssetDetected && isAttributeCacheable(n, attr.name)) {
if (value && onAssetDetected && isAttributeCapturable(n, attr.name)) {
if (attr.name === 'srcset') {
assets.push(...getUrlsFromSrcset(value));
} else {
Expand Down
6 changes: 3 additions & 3 deletions packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ export function getUrlsFromSrcset(srcset: string): string[] {
return urls;
}

export const CACHEABLE_ELEMENT_ATTRIBUTE_COMBINATIONS = new Map([
export const CAPTURABLE_ELEMENT_ATTRIBUTE_COMBINATIONS = new Map([
['IMG', new Set(['src', 'srcset'])],
['VIDEO', new Set(['src'])],
['AUDIO', new Set(['src'])],
Expand All @@ -378,8 +378,8 @@ export const CACHEABLE_ELEMENT_ATTRIBUTE_COMBINATIONS = new Map([
['cursor', new Set(['href'])],
]);

export function isAttributeCacheable(n: Element, attribute: string): boolean {
const acceptedAttributesSet = CACHEABLE_ELEMENT_ATTRIBUTE_COMBINATIONS.get(
export function isAttributeCapturable(n: Element, attribute: string): boolean {
const acceptedAttributesSet = CAPTURABLE_ELEMENT_ATTRIBUTE_COMBINATIONS.get(
n.nodeName,
);
if (!acceptedAttributesSet) {
Expand Down
12 changes: 6 additions & 6 deletions packages/rrweb-snapshot/test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @jest-environment jsdom
*/
import { isAttributeCacheable, isNodeMetaEqual } from '../src/utils';
import { isAttributeCapturable, isNodeMetaEqual } from '../src/utils';
import { NodeType, serializedNode, serializedNodeWithId } from '@rrweb/types';

describe('utils', () => {
Expand Down Expand Up @@ -147,7 +147,7 @@ describe('utils', () => {
});
});

describe('isAttributeCacheable()', () => {
describe('isAttributeCapturable()', () => {
const validAttributeCombinations = [
['img', ['src', 'srcset']],
['video', ['src']],
Expand Down Expand Up @@ -177,17 +177,17 @@ describe('utils', () => {
validAttributeCombinations.forEach(([tagName, attributes]) => {
const element = document.createElement(tagName);
attributes.forEach((attribute) => {
it(`should correctly identify <${tagName} ${attribute}> as cacheable`, () => {
expect(isAttributeCacheable(element, attribute)).toBe(true);
it(`should correctly identify <${tagName} ${attribute}> as capturable`, () => {
expect(isAttributeCapturable(element, attribute)).toBe(true);
});
});
});

invalidAttributeCombinations.forEach(([tagName, attributes]) => {
const element = document.createElement(tagName);
attributes.forEach((attribute) => {
it(`should correctly identify <${tagName} ${attribute}> as NOT cacheable`, () => {
expect(isAttributeCacheable(element, attribute)).toBe(false);
it(`should correctly identify <${tagName} ${attribute}> as NOT capturable`, () => {
expect(isAttributeCapturable(element, attribute)).toBe(false);
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ export default class MutationBuffer {
));
if (
transformedValue &&
this.assetManager.isAttributeCacheable(target, attributeName)
this.assetManager.isAttributeCapturable(target, attributeName)
) {
if (attributeName === 'srcset') {
getSourcesFromSrcset(transformedValue).forEach((url) => {
Expand Down
6 changes: 3 additions & 3 deletions packages/rrweb/src/record/observers/asset-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { encode } from 'base64-arraybuffer';
import { patch } from '../../utils';

import type { recordOptions } from '../../types';
import { isAttributeCacheable } from 'rrweb-snapshot';
import { isAttributeCapturable } from 'rrweb-snapshot';

export default class AssetManager {
private urlObjectMap = new Map<string, File | Blob | MediaSource>();
Expand Down Expand Up @@ -190,7 +190,7 @@ export default class AssetManager {
return { status: 'capturing' };
}

public isAttributeCacheable(n: Element, attribute: string): boolean {
return isAttributeCacheable(n, attribute);
public isAttributeCapturable(n: Element, attribute: string): boolean {
return isAttributeCapturable(n, attribute);
}
}
16 changes: 8 additions & 8 deletions packages/rrweb/src/replay/asset-manager/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {
captureAssetsParam,
} from '@rrweb/types';
import { deserializeArg } from '../canvas/deserialize-args';
import { getSourcesFromSrcset, isAttributeCacheable } from 'rrweb-snapshot';
import { getSourcesFromSrcset, isAttributeCapturable } from 'rrweb-snapshot';
import type { RRElement } from 'rrdom';
import { updateSrcset } from './update-srcset';

Expand Down Expand Up @@ -120,23 +120,23 @@ export default class AssetManager implements RebuildAssetManagerInterface {
};
}

public isCacheable(
public isCapturable(
n: RRElement | Element,
attribute: string,
value: string,
): boolean {
if (!isAttributeCacheable(n as Element, attribute)) return false;
if (!isAttributeCapturable(n as Element, attribute)) return false;

if (attribute === 'srcset') {
return getSourcesFromSrcset(value).some((source) =>
this.isURLOfCacheableOrigin(source),
this.isURLConfiguredForCapture(source),
);
} else {
return this.isURLOfCacheableOrigin(value);
return this.isURLConfiguredForCapture(value);
}
}

public isURLOfCacheableOrigin(url: string): boolean {
public isURLConfiguredForCapture(url: string): boolean {
if (url.startsWith('data:')) return false;

const { origins: cachedOrigins = false, objectURLs = false } =
Expand All @@ -163,7 +163,7 @@ export default class AssetManager implements RebuildAssetManagerInterface {
attribute: string,
): Promise<unknown> {
const originalValue = node.getAttribute(attribute);
if (!originalValue || !this.isCacheable(node, attribute, originalValue))
if (!originalValue || !this.isCapturable(node, attribute, originalValue))
return false;

const promises: Promise<unknown>[] = [];
Expand All @@ -172,7 +172,7 @@ export default class AssetManager implements RebuildAssetManagerInterface {
let expectedValue: string | void = originalValue;
const values = getSourcesFromSrcset(originalValue);
values.forEach((value) => {
if (!this.isURLOfCacheableOrigin(value)) return;
if (!this.isURLConfiguredForCapture(value)) return;
promises.push(
this.whenReady(value).then((status) => {
const isLoaded = status.status === 'loaded';
Expand Down
16 changes: 8 additions & 8 deletions packages/rrweb/test/replay/asset-unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ describe('AssetManager', () => {
},
);
urls.forEach((url) => {
it(`should correctly identify ${url} as cacheable for origin ${origin}`, () => {
expect(assetManager.isURLOfCacheableOrigin(url)).toBe(true);
it(`should correctly identify ${url} as capturable for origin ${origin}`, () => {
expect(assetManager.isURLConfiguredForCapture(url)).toBe(true);
});
});
});
Expand All @@ -195,8 +195,8 @@ describe('AssetManager', () => {
},
);
urls.forEach((url) => {
it(`should correctly identify ${url} as NOT cacheable for origin ${origin}`, () => {
expect(assetManager.isURLOfCacheableOrigin(url)).toBe(false);
it(`should correctly identify ${url} as NOT capturable for origin ${origin}`, () => {
expect(assetManager.isURLConfiguredForCapture(url)).toBe(false);
});
});
});
Expand Down Expand Up @@ -266,8 +266,8 @@ describe('AssetManager', () => {
objectURLs: false,
},
);
it(`should correctly identify <${element} ${attribute}=${value} /> as cacheable for origins ${origins}`, () => {
expect(assetManager.isCacheable(element, attribute, value)).toBe(true);
it(`should correctly identify <${element} ${attribute}=${value} /> as capturable for origins ${origins}`, () => {
expect(assetManager.isCapturable(element, attribute, value)).toBe(true);
});
});

Expand All @@ -281,8 +281,8 @@ describe('AssetManager', () => {
objectURLs: false,
},
);
it(`should correctly identify <${element} ${attribute}=${value} /> as NOT cacheable for origins ${origins}`, () => {
expect(assetManager.isCacheable(element, attribute, value)).toBe(false);
it(`should correctly identify <${element} ${attribute}=${value} /> as NOT capturable for origins ${origins}`, () => {
expect(assetManager.isCapturable(element, attribute, value)).toBe(false);
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,8 +759,8 @@ export declare abstract class RebuildAssetManagerInterface {
abstract get(url: string): RebuildAssetManagerStatus;
abstract whenReady(url: string): Promise<RebuildAssetManagerFinalStatus>;
abstract reset(config?: captureAssetsParam | undefined): void;
abstract isCacheable(n: Element, attribute: string, value: string): boolean;
abstract isURLOfCacheableOrigin(url: string): boolean;
abstract isCapturable(n: Element, attribute: string, value: string): boolean;
abstract isURLConfiguredForCapture(url: string): boolean;
abstract manageAttribute(n: Element, id: number, attribute: string): void;
}

Expand Down

0 comments on commit a88ff5e

Please sign in to comment.